package com.test.month_2020_04.day_03;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class T06_00_sync_wait_notify {


    public static void main(String[] args) {
        Object o = new Object();
        char[] c1 = "abcdefg".toCharArray();
        char[] c2 = "1234567".toCharArray();

        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();


        new Thread(()->{
            try {
                for (Character c : c1){
                    lock.lock();
                    System.out.println(c);
                    condition.signal();
                    condition.await();
                }
                condition.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }).start();


        new Thread(()->{
            try {
                for (Character c : c2){
                    lock.lock();
                    System.out.println(c);
                    condition.signal();
                    condition.await();
                }
                condition.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }).start();


        /*new Thread(()->{
            synchronized (o){
                for (Character c : c1){
                    System.out.println(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        },"t1").start();

        new Thread(()->{
            synchronized (o){
                for (Character c : c2){
                    System.out.println(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        },"t2").start();*/
    }
}